package com.bank.ui.admin;

import com.bank.dao.CustomerDAO;
import com.bank.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class EditCustomerDialog extends JDialog {
    private JTextField nameField;
    private JTextField pidField;
    private JTextField phoneField;
    private JTextField addressField;
    private int customerID;
    private String originalPID;
    private Runnable refreshCallback;
    //  对话框初始化时加载原始数据
    public EditCustomerDialog(int customerID, Runnable refreshCallback) {
        this.customerID = customerID;
        this.refreshCallback = refreshCallback;
        setTitle("修改客户");
        setSize(350, 250);
        setModal(true);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        // 内容面板
        JPanel contentPanel = new JPanel(new GridLayout(4, 2, 10, 10));
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        add(contentPanel, BorderLayout.CENTER);

        try {
            CustomerDAO customerDAO = new CustomerDAO();
            Customer customer = customerDAO.getCustomerByID(customerID);

            if (customer == null) {
                JOptionPane.showMessageDialog(this, "找不到该客户信息！", "错误", JOptionPane.ERROR_MESSAGE);
                dispose();
                return;
            }

            originalPID = customer.getPID();

            contentPanel.add(new JLabel("客户姓名:"));
            nameField = new JTextField(customer.getCustomerName());
            contentPanel.add(nameField);

            contentPanel.add(new JLabel("身份证号:"));
            pidField = new JTextField(customer.getPID());
            contentPanel.add(pidField);

            contentPanel.add(new JLabel("联系电话:"));
            phoneField = new JTextField(customer.getTelephone());
            contentPanel.add(phoneField);

            contentPanel.add(new JLabel("联系地址:"));
            addressField = new JTextField(customer.getAddress());
            contentPanel.add(addressField);

        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "加载客户信息失败: " + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
            dispose();
            return;
        }

        //保存按钮事件处理
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText().trim();
                String pid = pidField.getText().trim();
                String phone = phoneField.getText().trim();
                String address = addressField.getText().trim();

                if (name.isEmpty() || pid.isEmpty() || phone.isEmpty() || address.isEmpty()) {
                    JOptionPane.showMessageDialog(EditCustomerDialog.this,
                            "请填写完整信息！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 验证身份证号格式 (18位)
                if (pid.length() != 18 || !pid.matches("\\d{17}[0-9Xx]")) {
                    JOptionPane.showMessageDialog(EditCustomerDialog.this,
                            "身份证号格式不正确！必须是18位数字（最后一位可以是X）", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 验证电话号码格式
                if (!phone.matches("1[3-9]\\d{9}")) {
                    JOptionPane.showMessageDialog(EditCustomerDialog.this,
                            "电话号码格式不正确！必须是11位手机号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                try {
                    CustomerDAO customerDAO = new CustomerDAO();

                    // 检查PID是否被修改且是否重复
                    if (!pid.equals(originalPID)) {
                        if (customerDAO.getCustomerByPID(pid) != null) {
                            JOptionPane.showMessageDialog(EditCustomerDialog.this,
                                    "该身份证号已被使用！", "错误", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    }
                    // 更新数据库
                    Customer customer = new Customer();
                    customer.setCustomerID(customerID);
                    customer.setCustomerName(name);
                    customer.setPID(pid);
                    customer.setTelephone(phone);
                    customer.setAddress(address);

                    customerDAO.updateCustomer(customer);

                    JOptionPane.showMessageDialog(EditCustomerDialog.this,
                            "客户信息修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    dispose();

                    // 执行刷新回调， 刷新界面
                    if (refreshCallback != null) {
                        refreshCallback.run();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(EditCustomerDialog.this,
                            "修改失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(evt -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }
}